package cn.dengta.webapp.user.model;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nullable;

import cn.dengta.webapp.user.entity.User;
import lombok.Getter;
import me.codeplayer.util.StringUtil;
import org.apache.commons.lang3.text.StrSubstitutor;

@Getter
public class MailTemplate implements Serializable {

	public static final String VAR_CODE = "code";
	public static final String VAR_USER_NAME = "username";
	public static final String ALIAS_REGISTER = "register";
	//
	protected String code;
	protected String title;
	protected String template;
	//
	static final Map<String, MailTemplate> internal = new HashMap<>();

	public MailTemplate(String code, String template, String title) {
		init(code, template, title);
	}

	public MailTemplate init(String code, String template, String title) {
		this.code = code;
		this.title = title;
		this.template = template;
		return this;
	}

	public static MailTemplate getTemplate(String alias) {
		return internal.get(alias);
	}

	public static MailTemplate getTemplate(String alias, @Nullable String defaultAlias) {
		MailTemplate tpl = internal.get(alias);
		if (tpl == null && defaultAlias != null) {
			tpl = internal.get(defaultAlias);
		}
		return tpl;
	}

	public static void addTemplate(final String alias, final String templateCode, final String template) {
		internal.put(alias, new MailTemplate(templateCode, template, null));
	}

	@SuppressWarnings("deprecation")
	public String parse(Map<String, Object> context) {
		return StrSubstitutor.replace(template, context);
	}

	public MailContextTemplate toContextTemplate(final Map<String, Object> context) {
		return new MailContextTemplate(this, context);
	}

	public MailContextTemplate toContextTemplate(@Nullable User user, @Nullable String captcha) {
		final Map<String, Object> context = new HashMap<>(8);
		applyInternalVariables(context, user, captcha);
		return toContextTemplate(context);
	}

	public static Map<String, Object> applyInternalVariables(Map<String, Object> context, @Nullable User user, @Nullable String captcha) {
		if (captcha != null) {
			context.put(VAR_CODE, captcha);
		}
		if (user != null) {
			String name = user.getRealName_();
			if (StringUtil.isEmpty(name)) {
				name = user.getNickname();
			}
			if (StringUtil.isEmpty(name)) {
				name = user.getUsername_();
			}
			context.put(VAR_USER_NAME, name);
		}
		return context;
	}

	public static Map<String, Object> createContext(final Object... kvPairs) {
		if (kvPairs != null) {
			final Map<String, Object> context = new HashMap<>(kvPairs.length > 2 ? 8 : 4);
			for (int i = 0; i < kvPairs.length; i++) {
				String key = (String) kvPairs[i++];
				if (StringUtil.notEmpty(key)) {
					context.put(key, kvPairs[i]);
				}
			}
			return context;
		}
		return null;
	}

	static {
		addTemplate(ALIAS_REGISTER, "972713",
				"您已注册！请使用账号 ${username} 或当前接收短信的手机号码进行登录，登录密码为：${password}，请妥善保管！");
	}

	public static MailContextTemplate forRegister(User user, String tempPassword) {
		final Map<String, Object> context = createContext(
				"username", user.getUsername(),
				"password", tempPassword);
		return MailTemplate.getTemplate(ALIAS_REGISTER).toContextTemplate(context);
	}

}
